package copyengine.resource.resProxy.loadQueue
{
    import copyengine.resource.resProxy.CEResFIleDesc;
    import copyengine.resource.resProxy.cacheData.CEResCacheDataBasic;
    import copyengine.resource.resProxy.cacheData.CEResCacheDataFactory;
    import copyengine.resource.resProxy.cachePool.CEResCachePool;

    public class CEResLoadQueue
    {
        private var mAllResCacheDataBasicVector:Vector.<CEResCacheDataBasic>;

        private var mResProxyLoadFinishCallback:Function;
        private var mOnCompleteCallback:Function;
        private var mInProgressCallback:Function;
        private var mOnErrorCallback:Function;

        private var mTotalLoadedResNum:int = 0;

        public function CEResLoadQueue()
        {
        }

        private function dispose():void
        {
        }

        public function setLoadingData(_resVector:Vector.<CEResFIleDesc>, _onComplete:Function, _inProgress:Function = null, _onError:Function = null):void
        {
            mAllResCacheDataBasicVector = new Vector.<CEResCacheDataBasic>();
            mOnCompleteCallback = _onComplete;
            mInProgressCallback = _inProgress;
            mOnErrorCallback = _onError;

            for each(var fileDesc:CEResFIleDesc in _resVector) {
                var resCacheData:CEResCacheDataBasic = CEResCachePool.instance.getResCacheData(fileDesc.fileName);
                if (resCacheData == null) {
                    resCacheData = CEResCacheDataFactory.instance.createCEResCacheData(fileDesc);
                    CEResCachePool.instance.setResCacheData(fileDesc.fileName, resCacheData);
                }
                mAllResCacheDataBasicVector.push(resCacheData);
            }

        }

        public function setResProxyLoadFinishCallback(_resProxyLoadFinishCallback:Function):void
        {
            mResProxyLoadFinishCallback = _resProxyLoadFinishCallback;
        }

        public function start():void
        {
            for each(var resData:CEResCacheDataBasic in mAllResCacheDataBasicVector) {
                if (resData.isLoaded()) {
                    mTotalLoadedResNum++
                } else {
                    resData.loadRes(onResLoadComplete, onResLoadInProgress, onResLoadInError);
                }
            }
            tryToFinishLoad();
        }

        //========================//
        //==Private  Function
        //=========================//

        private function onResLoadComplete():void
        {
            mTotalLoadedResNum++;
            tryToFinishLoad();
        }

        private function onResLoadInProgress():void
        {
        }

        private function onResLoadInError():void
        {
        }

        private function tryToFinishLoad():void
        {
            if (mTotalLoadedResNum >= mAllResCacheDataBasicVector.length) {
                mResProxyLoadFinishCallback(this, mAllResCacheDataBasicVector);
                mOnCompleteCallback();
                dispose();
            }
        }

    }

}
